package com.kuzufang.modules.search;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.crystal.crystalrangeseekbar.widgets.CrystalRangeSeekbar;
import com.kuzufang.R;
import com.kuzufang.base.BaseActivity;
import com.kuzufang.base.BaseAdapter;
import com.kuzufang.utils.DisplayUtils;

/**
 * Created by songyuanjin on 2017/8/29.
 */

public class SearchActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar mToolBar;
    private RelativeLayout mTraffic;
    private LinearLayout mSubwayShowArea;
    private RecyclerView mSubWayLineRecyclerView;
    private SubWayLineAdapter mSubWayLineAdapter;
    private RecyclerView mSubWayStationRecyclerView;
    private SubWayStationGridAdapter mSubWayStationAdapter;

    private RelativeLayout mPrice;
    private LinearLayout mPriceShowArea;
    CrystalRangeSeekbar rangeSeekbar;

    private RelativeLayout mRoommate;
    private LinearLayout mRoommateShowArea;
    private RecyclerView mRoommateConfigRecyclerView;
    private RoommateConfigAdapter roommateConfigAdapter;

    private ImageView mTrafficArror;
    private ImageView mPriceArror;
    private ImageView mRoommateArror;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mToolBar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolBar);
        mToolBar.setTitle("");
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        initView();

        mSubWayLineRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mSubWayLineAdapter = new SubWayLineAdapter(this);
        mSubWayLineRecyclerView.setAdapter(mSubWayLineAdapter);

        mSubWayStationAdapter = new SubWayStationGridAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        gridLayoutManager.setAutoMeasureEnabled(true);
        mSubWayStationRecyclerView.setLayoutManager(gridLayoutManager);
        mSubWayStationRecyclerView.setAdapter(mSubWayStationAdapter);

        roommateConfigAdapter = new RoommateConfigAdapter(this);
        mRoommateConfigRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mRoommateConfigRecyclerView.setAdapter(roommateConfigAdapter);
        mRoommateConfigRecyclerView.setFocusable(false);

        mSubWayStationAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView.Adapter adapter, Object data, int position) {
                mSubwayShowArea.setVisibility(View.GONE);
            }
        });

        mTraffic.setOnClickListener(this);
        mPrice.setOnClickListener(this);
        mRoommate.setOnClickListener(this);
    }

    private void initView() {
        mPriceShowArea = (LinearLayout) findViewById(R.id.ll_price);
        rangeSeekbar = (CrystalRangeSeekbar) findViewById(R.id.rangeSeekbar1);
        mRoommate = (RelativeLayout) findViewById(R.id.rl_roommate);
        mRoommateShowArea = (LinearLayout) findViewById(R.id.ll_show_roommate);
        mRoommateConfigRecyclerView = (RecyclerView) findViewById(R.id.roommate_config);
        mPrice = (RelativeLayout) findViewById(R.id.rl_price);
        mTraffic = (RelativeLayout) findViewById(R.id.rl_traffic);
        mSubwayShowArea = (LinearLayout) findViewById(R.id.ll_subway);
        mTrafficArror = (ImageView) findViewById(R.id.iv_traffic_arror);
        mPriceArror = (ImageView) findViewById(R.id.iv_arror_price);
        mRoommateArror = (ImageView) findViewById(R.id.iv_arror_roommate);
        mSubWayLineRecyclerView = (RecyclerView) findViewById(R.id.subway_line);
        mSubWayStationRecyclerView = (RecyclerView) findViewById(R.id.subway_stations);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_traffic:
                if (mSubwayShowArea.getVisibility() == View.GONE) {
                    mSubwayShowArea.setVisibility(View.VISIBLE);
                    int expandHeight = DisplayUtils.measureHeight(mSubwayShowArea);
                    expandAnimation(mSubwayShowArea, mTrafficArror, expandHeight);
                } else {
                    condenseAnimation(mSubwayShowArea, mTrafficArror, 0);
                }
                break;
            case R.id.rl_price:
                if (mPriceShowArea.getVisibility() == View.GONE) {
                    mPriceShowArea.setVisibility(View.VISIBLE);
                    int expandHeight = DisplayUtils.measureHeight(mPriceShowArea);
                    expandAnimation(mPriceShowArea, mPriceArror, expandHeight);
                } else {
                    condenseAnimation(mPriceShowArea, mPriceArror, 0);
                }
                break;
            case R.id.rl_roommate:
                if (mRoommateShowArea.getVisibility() == View.GONE) {
                    mRoommateShowArea.setVisibility(View.VISIBLE);
                    int expandHeight = DisplayUtils.measureHeight(mRoommateShowArea);
                    expandAnimation(mRoommateShowArea, mRoommateArror, expandHeight);
                } else {
                    condenseAnimation(mRoommateShowArea, mRoommateArror, 0);
                }
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_confirm, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.search_confirm:
                Toast.makeText(this, "Confirm", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                onBackPressed();
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void expandAnimation(final LinearLayout expandArea, final ImageView arror, int expandHeight) {
        expandArea.setVisibility(View.VISIBLE);
        final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) expandArea.getLayoutParams();
        ValueAnimator heightAnimator = ValueAnimator.ofInt(0, expandHeight).setDuration(300);
        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentHeight = (int) animation.getAnimatedValue();
                layoutParams.height = currentHeight;
                expandArea.setLayoutParams(layoutParams);
            }
        });

        ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(arror, "rotation", 0f, 180f);
        rotationAnimator.setDuration(300);
        AnimatorSet animationSet = new AnimatorSet();
        animationSet.playTogether(heightAnimator, rotationAnimator);
        animationSet.start();

    }

    private void condenseAnimation(final LinearLayout expandArea, final ImageView arror, int condenseHeight) {
        final int expandHeight = DisplayUtils.measureHeight(expandArea);
        final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) expandArea.getLayoutParams();
        ValueAnimator heightAnimator = ValueAnimator.ofInt(expandHeight, 0).setDuration(300);
        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentHeight = (int) animation.getAnimatedValue();
                layoutParams.height = currentHeight;
                expandArea.setLayoutParams(layoutParams);
            }
        });
        heightAnimator.addListener(new SimpleAnimationListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                layoutParams.height = expandHeight;
                expandArea.setLayoutParams(layoutParams);
                expandArea.setVisibility(View.GONE);
            }
        });

        ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(arror, "rotation", 180f, 0f);
        rotationAnimator.setDuration(300);
        AnimatorSet animationSet = new AnimatorSet();
        animationSet.playTogether(heightAnimator, rotationAnimator);
        animationSet.start();
    }

}
